package com.byd.dms.fastjson;

import com.alibaba.fastjson.JSON;
import com.byd.dms.entity.Dept;
import com.byd.dms.entity.User;
import com.byd.dms.entity.User2;

import java.util.*;

public class FastDemo3_1 {
    private static final Dept deptProp;
    private static final User userProp;
    private static final User2 user2Prop;
    
    static {
        userProp = new User("刘阿勇", 18, "男");
        Dept dept2 = new Dept(5L, "测试部", 52);
        userProp.setDept(dept2);
        userProp.setHobbies(Arrays.asList("吃饭", "睡觉", "乌拉圈"));
        
        deptProp = new Dept(3L, "开发部", 38);
        User user1 = new User("秦疆1号", 3, "男");
        User user2 = new User("秦疆2号", 3, "男");
        User user3 = new User("秦疆3号", 3, "男");
        
        List<User> list = new ArrayList<User>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        
        deptProp.setUsers(list);
        
        user2Prop = new User2("韩梅梅", 22, "女");
    }
    
    public static void main(String[] args) {
        
        listToMap();
    }
    
    // todo listToMap
    private static void listToMap() {
        user2Prop.getMap().add(new Dept(6L, "产品", 62));
        user2Prop.getMap().add(new Dept(7L, "运营", 32));
        
        //方法一  配置， User2序列化map结构， User就给以直接解析了 User2 @JSONField(serializeUsing = ListToMapSerializer.class)
        String jsonStr = JSON.toJSONString(user2Prop);
        System.out.println("jsonStr = " + jsonStr);
        
        
        // 方法二 配置 User反序列化， 让json的List 解析为Map结构  User1 @JSONField(deserializeUsing  = ListToMapSerializer.class)
        
        //解析时json将list转为map User2.map ===> User.map List --->  Map
        User user = JSON.parseObject(jsonStr, User.class);
        System.out.println("user = " + user);
        
    }
    
    // todo mapToList
    private static void mapToList() {
        Map<Integer, Dept> map = new HashMap<>();
        map.put(0, new Dept(6L, "产品", 62));
        map.put(1, new Dept(7L, "运营", 32));
        userProp.setMap(map);
        String jsonStr = JSON.toJSONString(userProp);
        System.out.println("jsonStr = " + jsonStr);
        
        //JSONObject jsonObject1 = (JSONObject) JSON.toJSON(user);
        //System.out.println("map: " + jsonObject1.getString("map"));
        
        //解析时json将map转为list User.map ===> User2.map  Map ---> List
        
        // 直接转换报错
        User2 user2 = JSON.parseObject(jsonStr, User2.class);
        System.out.println("user2 = " + user2);
        LinkedList<Dept> map1 = user2.getMap();
        System.out.println("map1 = " + map1);
    }
}
